
function initialWindow() {

	// if open by other browser
	var viewResult = jQuery.parseJSON($.cookie("viewResult"));
	if (viewResult != undefined && viewResult != null) {
		$("#uri").val(viewResult.uri);
		$("#datatype").attr("value", viewResult.datatype);

		$("#showResultButton").trigger("click");
	}
	$.cookie("viewResult", null, {
		path : '/'
	});// clear cookie
	// end of this part
}

$(function() {

	$("#datatype").buttonset();

	$("input[type=radio]").click(function() {
		$("#datatype").attr("value", $(this).attr("value"));
	});

	$("#showResultButton").button().click(function() {

		var link = $("#uri").val();

		var Accept = $("#datatype").attr("value");

		$.ajax({
			url : link,
			type : "get",
			crossDomain : true,
			contentType : "application/json; charset=utf-8",
			dataType : "html",
			headers : {
				"Accept" : Accept
			},
			success : function(data) {
				$("#result").html("");
				var codeArea = $("#result").codeArea(data);
				// if has link feedback

				var s = data.split('\n');
				$.each(s, function() {
					var num = this.indexOf("link\/feedback");
					
					if (num != -1) {
						var result=this.substring(this.indexOf("http"),this.lastIndexOf("\""));
						//$("#result").codeArea(result);
						$.ajax({
							url : result,
							type : "get",
							crossDomain : true,
							contentType : "application/json; charset=utf-8",
							dataType : "html",
							headers : {
								"Accept" : Accept
							},
							success : function(data) {
								$("#result").codeArea(data);
							}
							});
						
					}
				});

			},
			error : function(data) {
				$("#result").prepend("<div>url error</div>");
			},
			beforeSend : function() {
				$("#result").html("");
			}
		});
	});
	initialWindow();

});